/**
 * Copyright (c) 2009 Jim Hurne
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package org.tzconverter.factory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.TimeZone;

/**
 * A TimeZoneFactory that maps the common three-letter time zone abbreviations
 * to full time zone names (EST = America/New_York) before getting the time zone.
 * <p>
 * If a full time zone name is provided, then no mapping is performed, and the behavior
 * of the <code>getTimeZone</code> method is identical the <code>TimeZone.getTimeZone</code>.
 * </p>
 * @since Apr 18, 2009
 */

public class MappingTimeZoneFactory implements TimeZoneFactory
{
  private static final String PROPERTIES_FILE_NAME = "TimeZoneMap.properties";
  
  private Properties tzMap;
  
  /**
   * Constructs a new 
   */
  public MappingTimeZoneFactory() {
    InputStream in = MappingTimeZoneFactory.class.getResourceAsStream(PROPERTIES_FILE_NAME);
    
    tzMap = new Properties();
    try
    {
      tzMap.load(in);
    }
    catch (IOException e)
    {
      //If an IOException happens here, it is ether a programming error
      //or a serious problem.  Wrap the exception in a RuntimeException and rethrow.
      throw new RuntimeException("IOException while loading mapped properties.", e);
    }
  }
  
  /*
   * @see org.tzconverter.TimeZoneFactory#getTimeZone(java.lang.String)
   */
  public TimeZone getTimeZone(String timeZoneCode)
  {
    timeZoneCode = tzMap.getProperty(timeZoneCode, timeZoneCode);
    return TimeZone.getTimeZone(timeZoneCode);
  }
  
}
